package com.house.api.controller;

import com.house.common.context.BaseContext;
import com.house.common.context.RedisContext;
import com.house.common.exception.HouseException;
import com.house.common.result.HttpCodeEnum;
import com.house.common.result.Result;
import com.house.model.dto.UserLoginDto;
import com.house.model.entity.User;
import com.house.model.vo.UserLoginVO;
import com.house.model.vo.UserSubscribeLevelVo;
import com.house.service.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
@Api(tags = "用户相关接口")
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDto dto){
        UserLoginVO userLoginVO = userService.wxLogin(dto);
        return Result.ok(userLoginVO);
    }

    @GetMapping("/code")
    @ApiOperation("获取验证码")
    public Result<String> code(String phone) {
        int code = (int) (Math.random() * 1000000);
        redisTemplate.opsForValue().set(RedisContext.LOGIN_CODE+phone,code,60, TimeUnit.SECONDS);
        log.info("code = {}",code);
        return Result.ok(code);
    }

    @GetMapping("/iscode")
    @ApiOperation("判断验证码")
    public Result isCode(int code,String phone) {
        Object object = redisTemplate.opsForValue().get(RedisContext.LOGIN_CODE+phone);
        if (object == null){
            throw new HouseException(HttpCodeEnum.LOGIN_CODE_ERROR);
        }
        int redisCode = (int) object;
        if (redisCode == code) {
            return Result.ok();
        }else {
            return Result.error(HttpCodeEnum.LOGIN_CODE_ERROR);
        }
    }

    @GetMapping("/userInfo")
    @ApiOperation("查询用户信息")
    public Result getUserInfo() {
        User userInfo = userService.getUserInfo();
        return Result.ok(userInfo);
    }

    @GetMapping("/islogin")
    @ApiOperation("判断是否登录")
    public Result isLogin() {
        String userId = BaseContext.getCurrentId();
        return Result.ok(userId);
    }

    //获取修改页面的初始数据
    @GetMapping("/userById")
    public Result userById(){
        String userId = BaseContext.getCurrentId();
        //根据ID获取user资料
        User user = userService.getById(userId);
        return Result.ok(user);
    }


    @PutMapping("/updataUser")
    @ApiOperation("修改用户")
    private Result upDataUser(@RequestBody User user){
        userService.updateByUser(user);
        return Result.ok();

    }


    //获取修改密码的初始数据z
    @GetMapping("/userPass")
    public Result userPass(){
        String userId = BaseContext.getCurrentId();

        User user = userService.getById(userId);

        return Result.ok(user);
    }


    @PutMapping("/updateUserPassWord/{password}")
    @ApiOperation("修改密码")
    private Result updateUserPassWord(@PathVariable("password") String password){
        userService.updateByUserPassWord(password);
        return Result.ok();

    }


    @GetMapping("/getUserSubscribe/{houseId}")
    public Result getUserSubscribeLevel(@PathVariable("houseId") String houseId){
        UserSubscribeLevelVo userSubscribeLevel = userService.getUserSubscribeLevel(houseId);
        return Result.ok(userSubscribeLevel);
    }

}
